home *** CD-ROM | disk | FTP | other *** search
/ L' Effet Pommier 3 / L'Effet Pommier - Volume 03.iso / Programmation / Alpha ƒ / Tcl / SystemCode / wordCompletion.tcl < prev   
Text File  |  1996-01-06  |  6KB  |  197 lines

  1. ####################################################################
  2. #  FILE: "wordCompletion+.tcl"
  3. #                                    created: 26/11/95 {1:05:48 am} 
  4. #                                last update: 26/11/95 {1:06:49 am} 
  5. #  Author: Vince Darley
  6. #  E-mail: <mailto:vince@das.harvard.edu>
  7. #    mail: Division of Applied Sciences, Harvard University
  8. #          Oxford Street, Cambridge MA 02138, USA
  9. #  <http://www.das.harvard.edu/users/students/Vincent_Darley/>
  10. #  
  11. #  Description: Modified procedure allowing for a different
  12. #                 search string for completion.
  13. #  History
  14. #  Based on Mark Nagata's Word Completion, w/ additions from Tim van der Leeuw
  15. #
  16. #  modified by  rev reason
  17. #  -------- --- --- -----------
  18. #  26/11/95 VMD 1.0 original
  19. #  _/_/_
  20. #
  21. #
  22. # From original documentation:
  23. #   This extension saves typing, as well as making sure variable names are 
  24. #   correct.  While typing the following C code, there is no need to type all 
  25. #   of the second occurrence of verySpecialInt.  One could copy and paste, but 
  26. #   another way is to type a few letters, and select the Word Completion 
  27. #   Extension.
  28. #   
  29. #       int verySpecialInt = 10;
  30. #       while(verySp                                                    
  31. #   
  32. #   
  33. #   becomes
  34. #   
  35. #       int verySpecialInt = 10;
  36. #       while(verySpecialInt                                             
  37. #   
  38. #                                                       
  39. #   
  40. #   Word Completion will look back in the code to find the first match and then 
  41. #   extend the current occurrence to match the previous occurrence.  If a match 
  42. #   is not found looking backwards, then it looks forwards.  If not found 
  43. #   forwards, the word is selected.  This is a quick way to save on typing, 
  44. #   good for helping prevent RSI, but perhaps more importantly, to make sure 
  45. #   that variable names are spelt correctly.
  46. #
  47. ####################################################################
  48.  
  49. set __wc__insPos -1
  50. proc wordCompletion { {got ""} {looking ""} } {
  51.     global __wc__len __wc__prevPos __wc__insPos __wc__prevFound __wc__pat __wc__nextStart __wc__fwd
  52.     
  53.     set pos [getPos]
  54.     # Cursor changed place?
  55.     if $pos==$__wc__insPos {
  56.         # it is an old search
  57.         set ret [wc__newSearch $pos]
  58.         if { $ret == 1 } {
  59.             return 
  60.         } elseif { $ret == -1 } {
  61.             select [expr $pos + [string length $looking] - [string length $__wc__prevFound] - [string length $got]] $pos
  62.             return
  63.         }
  64.     }
  65.     # Start new search for WordCompletion
  66.     if { $got == "" } {    
  67.         # this is a normal completion
  68.         backwardWord
  69.         set start [getPos]
  70.         set one [getText $start $pos]
  71.         set __wc__len [string length $one]
  72.         set __wc__pat [append one {[a-zA-Z0-9_]+}]
  73.     } else {
  74.         # here we complete 'got' with something beginning 'looking'
  75.         set start [expr $pos - [string length $got]]
  76.         set one $looking
  77.         set __wc__len [string length $one]
  78.         set __wc__pat [append one {[^ \t\n\r]+}]
  79.     }    
  80.     incr start -1
  81.  
  82.     if {![catch {search -s -f 0 -r 1 -i 0 -m 1 -- $__wc__pat $start} data]} {
  83.         set d00 [lindex $data 0]
  84.         set beg [expr $d00+$__wc__len]
  85.         set end [lindex $data 1]
  86.         set __wc__prevFound [getText $d00 $end ]
  87.         set txt [getText $beg $end]
  88.         goto $pos
  89.         insertText $txt
  90.         message "Found above."
  91.         # Set a number of globals for possible next go-around
  92.         set __wc__insPos [getPos]
  93.         set __wc__prevPos $pos
  94.         set __wc__nextStart [expr $d00-$__wc__len]
  95.         set __wc__fwd 0
  96.         return
  97.     }
  98.     if {![catch {search -s -f 1 -r 1 -i 0 -m 1 -- $__wc__pat $pos} data]} {
  99.         set __wc__prevFound [getText [lindex $data 0] [lindex $data 1] ]
  100.         set beg [expr [lindex $data 0]+$__wc__len]
  101.         set end [lindex $data 1]
  102.         set txt [getText $beg $end]
  103.         goto $pos
  104.         insertText $txt
  105.         message "Found below."
  106.         # Set a number of globals for possible next go-around
  107.         set __wc__insPos [getPos]
  108.         set __wc__prevPos $pos
  109.         set __wc__nextStart $end
  110.         set __wc__fwd 1
  111.         return
  112.     }
  113.     goto $pos
  114.     #backwardWordSelect
  115.     select [expr $start +1] $pos
  116. }
  117.  
  118. # returns '1' if it succeeded 
  119. # or -1 if failed completely
  120.  
  121. proc wc__newSearch { pos } {
  122.     global __wc__len __wc__prevPos __wc__insPos __wc__prevFound __wc__pat __wc__nextStart __wc__fwd
  123.     
  124.     set skipStr $__wc__prevFound
  125.     while 1 {    
  126.         if $__wc__fwd {
  127.             set fndMsg "Found below."
  128.         } else {
  129.             set fndMsg "Found above."
  130.         }
  131.         if {![catch {search -s -f $__wc__fwd -r 1 -i 0 -m 1 -- $__wc__pat $__wc__nextStart} data]} {
  132.             set d00 [lindex $data 0]
  133.             set beg [expr $d00+$__wc__len]
  134.             set end [lindex $data 1]
  135.             set __wc__prevFound [getText $d00 $end]
  136.             if [string compare $skipStr $__wc__prevFound] {
  137.                 # Have we got the same word twice?
  138.                 set txt [getText $beg $end]
  139.                 deleteText $__wc__prevPos $__wc__insPos
  140.                 goto $__wc__prevPos
  141.                 insertText $txt
  142.                 message $fndMsg
  143.                 # Set a number of globals for possible next go-around
  144.                 set __wc__insPos [getPos]
  145.                 if $__wc__fwd {
  146.                     # Search Forwards
  147.                     set __wc__nextStart $end
  148.                     # End of found word
  149.                 } else {
  150.                     # Search Backwards
  151.                     set __wc__nextStart [expr $d00 - $__wc__len]
  152.                     # Before start of found word
  153.                     if $__wc__nextStart<=0 {
  154.                         set __wc__fwd 1
  155.                         set __wc__nextStart $__wc__insPos
  156.                     }
  157.                 }
  158.                 return 1
  159.             } else {
  160.                 # Move start of search after finding string again
  161.                 if $__wc__fwd {
  162.                     # Searching Forwards
  163.                     set __wc__nextStart $end
  164.                     # End of found word
  165.                 } else {
  166.                     # Still Searching Backwards
  167.                     set __wc__nextStart [expr $d00 - $__wc__len]
  168.                     # Before start of found word
  169.                     if $__wc__nextStart<=0 {
  170.                         set __wc__fwd 1
  171.                         set __wc__nextStart $__wc__insPos
  172.                     }
  173.                 }
  174.             }
  175.             # End if string compare 
  176.         } else {
  177.             # Search string not found
  178.             if $__wc__fwd {
  179.                 # We were already looking forward, so the word is not in the file
  180.                 message "Not found."
  181.                 set __wc__insPos -1
  182.                 goto $pos
  183.                 return -1
  184.             } else {
  185.                 # start looking forward
  186.                 set __wc__fwd 1
  187.                 set __wc__nextStart $__wc__insPos
  188.             }
  189.         }
  190.         
  191.     }
  192.     return 0
  193. }
  194.